package cn.autumnorange.app.common.provider.security;

import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

// @EnableJpaRepositories(repositoryBaseClass = BaseJpaRepositoryImpl.class)
@EnableGlobalMethodSecurity(prePostEnabled = true) // 方法上开启注解权限方式
@EnableResourceServer
@EnableOAuth2Sso
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {

  @Override
  public void configure(HttpSecurity http) throws Exception {
    http.
//            启用跨域支持CorsFilter
            cors()
//        禁用cors跨域支持
            .disable()
//            .and()
        .csrf()
        .disable()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.NEVER)
        .and()
        .authorizeRequests()
        //        .antMatchers("/**")
        //        .authenticated()
        //                .antMatchers(HttpMethod.GET, "/test")
        //                .hasAuthority("ROLE_admin")
        .anyRequest()
        .permitAll();
    //            .antMatchers("/add")
    //            .authenticated();
  }
//  @Bean
//  public CorsConfigurationSource corsConfigurationSource() {
//    CorsConfiguration configuration = new CorsConfiguration();
//    configuration.setAllowedOrigins(Arrays.asList("http://localhost:8082","http://localhost:3000","http://localhost:3002"));
//    configuration.setAllowedMethods(Arrays.asList("*"));
//    configuration.addAllowedHeader("*");
//    configuration.setAllowCredentials(true);
//
//    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//    source.registerCorsConfiguration("/**", configuration);
//    return source;
//  }
}
